<?php

/* @var $this \yii\web\View */
/* @var $content string */

use app\widgets\Alert;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;
use app\models\Menu;
use app\models\Department;
use app\models\Job;

AppAsset::register($this);

?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">

<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php $this->registerCsrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
    <style type="text/css">
        .wrap>.container {
            padding: 0;
        }
    </style>
</head>

<body>
    <?php $this->beginBody() ?>

    <div class="wrap">
        <div id="page_header" style="height:54px;width:100%;position:relative;clear:both">
            <?php

            NavBar::begin([
                'brandLabel' => Yii::$app->name,
                'brandUrl' => Yii::$app->homeUrl,
                'options' => [
                    'class' => 'navbar-inverse navbar-fixed-top',
                ],
            ]);

            if (Yii::$app->user->id != null) {
                $menus = Menu::getUserRootMenu(0);
                $left_menus = [];
                if (null !=  $menus && count($menus) > 0) {
                    foreach ($menus as $m) {
                        $left_menus[] = ['label' => $m['name'], 'url' => [$m['url']]];
                    }
                }
                echo Nav::widget([
                    'options' => ['class' => 'navbar-nav'],
                    'items' => $left_menus,
                ]);
            }


            if (Yii::$app->user->id == null) {
                $items = [
                    Yii::$app->user->isGuest ? (['label' => '登录', 'url' => ['/site/login']]) : ('<li>'
                        . Html::beginForm(['/site/logout'], 'post')
                        . Html::submitButton(
                            '退出 (' . Yii::$app->user->identity->showname . ')',
                            ['class' => 'btn btn-link logout']
                        )
                        . Html::endForm()
                        . '</li>')
                ];
            } else {

                $menus = Menu::getUserRootMenu(-8);
                if (null !=  $menus && count($menus) > 0) {
                    $drop_menus = [];
                    foreach ($menus as $m) {
                        $drop_menus[] = ['label' => $m['name'], 'url' => [$m['url']]];
                    }
                    $items[] = ['label' => '设置', 'items' => $drop_menus];
                }

                if (Yii::$app->user->identity->jobModel->check_menu(69)) {
                    $departments = Department::getTreeData();
                    if (null !=  $departments && count($departments) > 0) {
                        $drop_departments = [];
                        foreach ($departments as $m) {
                            $drop_departments[] = ['label' => $m['name'], 'url' => 'javascript:select_department(' . $m['id'] . ');void(0)'];
                        }
                        $items[] = ['label' => '点选科室', 'items' => $drop_departments];
                    }
                }

                $items[] = ['label' => '修改密码', 'url' => 'javascript:modify_user_password();void(0)'];
                $items[] = Yii::$app->user->isGuest ? (['label' => '登录', 'url' => ['/site/login']]) : ('<li>'
                    . Html::beginForm(['/site/logout'], 'post')
                    . Html::submitButton(
                        '退出 (' . Yii::$app->user->identity->showname . ')',

                        ['class' => 'btn btn-link logout']
                    )
                    . Html::endForm()
                    . '</li>');
            }
            echo Nav::widget([
                'options' => ['class' => 'navbar-nav navbar-right'],
                'items' => $items,
            ]);
            NavBar::end();
            ?>
        </div>

        <div id="page_body" class="container" style="width: 100%;">
            <table style="width:100%;min-height:100%;">
                <tr>
                    <?php if (!Yii::$app->user->isGuest && null != $this->params['left_nav_menus'] && count($this->params['left_nav_menus']) > 0) { ?>
                        <td width="200" style="background-color: #bdd0db;position:relative;height:100%;">
                            <div id="body_left_nav" style="background-color: #f1f8fc;position: absolute;top:0;width:199px;">
                                <ul class="nav nav-pills nav-stacked">
                                    <?php foreach ($this->params['left_nav_menus'] as $m) { ?>
                                        <li role="presentation"><a href="<?= Url::toRoute($m['url']); ?>"><?= $m['name'] ?></a></li>
                                    <?php } ?>
                                </ul>
                            </div>
                        </td>
                    <?php } ?>
                    <td>
                        <div id="body_content" style="position: absolute;top:0;padding:0 10px;<?php if (Yii::$app->user->isGuest) {
                                                                                                    echo 'width:600px;left:50%;margin-left:-300px';
                                                                                                } else {
                                                                                                    echo 'width:100%';
                                                                                                } ?>">
                            <?= Breadcrumbs::widget([
                                'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
                            ]) ?>
                            <?= Alert::widget() ?>
                            <?= $content ?>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <?php $this->endBody() ?>
</body>

</html>
<?php $this->endPage() ?>
<script type="text/javascript">
    var page_header = document.getElementById("page_header");
    var page_body = document.getElementById("page_body");
    var body_left_nav = document.getElementById("body_left_nav");
    var body_content = document.getElementById("body_content");

    function changeBodyWidth() {
        page_body.style.height = (document.body.scrollHeight - page_header.offsetHeight) + 'px';
        if (null != body_left_nav) {
            body_left_nav.style.height = page_body.style.height;
            body_content.style.width = (document.body.clientWidth - body_left_nav.clientWidth) + 'px';
        }

    }
    changeBodyWidth();
    window.setTimeout("window.onresize = function(){changeBodyWidth()};changeBodyWidth();", 200);

    function select_department(department_id) {
        $.getJSON("<?= Url::toRoute('user/selectdepartment') ?>&id=" + department_id, function(res) {
            if (res.status == 'success') {
                layui.layer.msg(res.msg, {
                    icon: 1,
                    time: 3000 //1秒关闭（如果不配置，默认是3秒）
                });
                return window.location.reload();
            }
        });
    }

    function modify_user_password() {
        layui.layer.prompt({
            formType: 1,
            title: '请输入新密码',
            area: ['500px', '80px'] //自定义文本域宽高
        }, function(value, index, elem) {
            $.getJSON("<?= Url::toRoute('user/modifymyselfpassword') ?>&pwd=" + value, function(res) {
                if (res.status == 'success') {
                    layui.layer.msg(res.msg, {
                        icon: 1,
                        time: 2000 //1秒关闭（如果不配置，默认是3秒）
                    });
                }
            });
            layui.layer.close(index);
        });
    }

    $(function() { //禁用“确认重新提交表单”
        window.history.replaceState(null, null, window.location.href);
    })
</script>